package com.houfukude.Draw;

import java.util.ArrayList;
import java.util.Iterator;


import Utils.File_Utils;
import Utils.Note_Utils;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Path;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

public class ImageDraw extends ImageView {

	private String DEBUG_TAG = "ImageDraw";

	private String src = null;
	private Bitmap bitmap = null;
	// 屏幕长宽
	private int screenWidth;
	private int screenHeight;
	private Canvas canvas;
	// 画布的画笔
	private Paint bitmapPaint;
	// 真实的画笔
	private Paint paint;
	// 保存Path路径的集合,用List集合来模拟栈
	private ArrayList<DrawPath> savePath;
	private Path path;
	// 临时点坐标
	private float x;
	private float y;
	private static final float TOUCH_TOLERANCE = 4;
	private DrawPath dp;

	public ImageDraw(Context context, int screenWidth, int screenHeight,
			String src) {
		super(context);
		initializeView(context, screenWidth, screenHeight, src);

	}

	public void initializeView(Context context, int screenWidth,
			int screenHeight, String src) {
		this.screenWidth = screenWidth;
		this.screenHeight = screenHeight;
		this.src = src;

		if (src != null) {
			bitmap = Note_Utils.getImageZoom(src, screenWidth, screenHeight);
			bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
			Log.d(DEBUG_TAG, "bitmap.W:" + bitmap.getWidth() + " bitmap.H:"
					+ bitmap.getHeight());
			Log.d(DEBUG_TAG, "screenWidth:" + screenWidth + " screenHeight:"
					+ screenHeight);

		} else {

			bitmap = Bitmap.createBitmap(screenWidth, screenHeight,
					Bitmap.Config.ARGB_8888);
		}
		// 保存一次一次绘制出来的图形
		canvas = new Canvas(bitmap);
		bitmapPaint = new Paint(Paint.DITHER_FLAG);
		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeJoin(Paint.Join.ROUND);// 设置外边缘
		paint.setStrokeCap(Paint.Cap.SQUARE);// 形状
		paint.setStrokeWidth(5);// 画笔宽度

		savePath = new ArrayList<DrawPath>();

	}

	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawColor(0xFFAAAAAA);
		// 将前面已经画过得显示出来
		canvas.drawBitmap(bitmap, 0, 0, bitmapPaint);
		if (path != null) {
			// 实时的显示
			canvas.drawPath(path, paint);
		}
		// super.onDraw(canvas);
	}

	@Override  
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
    }

	

	private void start(float x, float y) {
		path.moveTo(x, y);
		this.x = x;
		this.y = y;
	}

	private void move(float x, float y) {
		float tmpx = Math.abs(x - this.x);
		float tmpy = Math.abs(this.y - y);
		if (tmpx >= TOUCH_TOLERANCE || tmpy >= TOUCH_TOLERANCE) {
			path.quadTo(this.x, this.y, (x + this.x) / 2, (y + this.y) / 2);
			this.x = x;
			this.y = y;
		}
	}

	private void up() {
		path.lineTo(this.x, this.y);
		canvas.drawPath(path, paint);
		savePath.add(dp);
		path = null;
	}

	/**
	 * 撤销方法
	 * 
	 * @return 用于判断是否是最后一次撤销操作
	 */
	public boolean undo() {

		boolean flag = false;
		if (src != null) {
			bitmap = Note_Utils.getImageZoom(src, screenWidth, screenHeight);
			bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
		} else {
			bitmap = Bitmap.createBitmap(screenWidth, screenHeight,
					Bitmap.Config.ARGB_8888);
		}
		canvas = new Canvas(bitmap);

		if (savePath != null && savePath.size() > 0) {
			// 移除最后一个path,相当于出栈操作
			savePath.remove(savePath.size() - 1);
			flag = true;
		}
		Iterator<DrawPath> iterator = savePath.iterator();
		while (iterator.hasNext()) {
			DrawPath drawPath = iterator.next();
			canvas.drawPath(drawPath.path, drawPath.paint);
		}
		invalidate();
		return flag;
	}

	public void redo() {

	}

	/**
	 * 
	 * @return 返回保存路径
	 */
	public String save(Context context) {
		String name = File_Utils.getNameFromPath(src);
		File_Utils.DeleteOldFile();
		return File_Utils.saveImageFile(context, name, bitmap);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		float x = event.getX();
		float y = event.getY();
		// Log.i(DEBUG_TAG, "x :"+x+" y :"+y);
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			path = new Path();
			dp = new DrawPath();
			dp.path = path;
			dp.paint = paint;
			start(x, y);
			invalidate();
			break;
		case MotionEvent.ACTION_MOVE:
			move(x, y);
			invalidate();
			break;
		case MotionEvent.ACTION_UP:
			up();
			invalidate();
			break;
		default:
			break;
		}
		return true;// super.onTouchEvent(event);
	}

}
